package co2.bean;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import co2.engine.DAOInterface;
import co2.pojos.BankMotivasi;
import co2.pojos.BankSoal;
import co2.pojos.PaketMotivasi;
import co2.pojos.PaketSoal;


@ManagedBean
@ViewScoped
public class ListBMBean extends AbstractBean implements Serializable {
	private static final long serialVersionUID = 1L;

	private PaketMotivasi selectedPS;
	private List<BankMotivasi> list;
	private BankMotivasi bankso;
	private BankMotivasi selectedBankso;
	
	@Autowired
	@Qualifier("daoI")
	private DAOInterface dao;
	private SignInBean signInBean;
	
	@PostConstruct
	public void postCon(){
		try{
			bankso = new BankMotivasi();
			Map map = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
			signInBean = (SignInBean) map.get("signInBean");
			
			selectedPS = (PaketMotivasi) signInBean.getMap().get("paketSoalMotivasi");
			list = dao.getSoalByPaketMotivasi(selectedPS.getId());
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public void delete(){
		try{
			dao.delete(BankMotivasi.class, bankso.getId());
			bankso = new BankMotivasi();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Deleted",""));
			int jml = selectedPS.getJumlah();
			jml --;
			selectedPS.setJumlah(jml);
			dao.update(selectedPS);
			
			list = dao.getSoalByPaketMotivasi(selectedPS.getId());
		}catch(Exception e){
			e.printStackTrace();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error Delete",""));
		}
	}
	public void edit(){
		try{
			signInBean.getMap().put("bankM", bankso);
			signInBean.getMap().put("editFlag", "Y");
			
			String path = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
			FacesContext.getCurrentInstance().getExternalContext().redirect(path+"/jsf/target/crud_bank_motivasi.jsf");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	//getter setter
	
	public DAOInterface getDao() {
		return dao;
	}
	public PaketMotivasi getSelectedPS() {
		return selectedPS;
	}
	public void setSelectedPS(PaketMotivasi selectedPS) {
		this.selectedPS = selectedPS;
	}
	public List<BankMotivasi> getList() {
		return list;
	}
	public void setList(List<BankMotivasi> list) {
		this.list = list;
	}
	public BankMotivasi getBankso() {
		return bankso;
	}
	public void setBankso(BankMotivasi bankso) {
		this.bankso = bankso;
	}
	public BankMotivasi getSelectedBankso() {
		return selectedBankso;
	}
	public void setSelectedBankso(BankMotivasi selectedBankso) {
		this.selectedBankso = selectedBankso;
	}
	public SignInBean getSignInBean() {
		return signInBean;
	}
	public void setSignInBean(SignInBean signInBean) {
		this.signInBean = signInBean;
	}
	public void setDao(DAOInterface dao) {
		this.dao = dao;
	}
}
